# -*- coding: utf-8 -*-
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django.views.generic.list import ListView
from documents.forms import DocAddForm, CatAddForm
from documents.models import DocCategory, Document

class DocList(ListView):
    template_name = 'docs/doc_list.html'
    def get_queryset(self):
        if self.request.user.is_superuser:
            return DocCategory.objects.all()
        else:
            return DocCategory.objects.filter(access__exact=self.request.user)


class DocstbyCategory(DetailView):
    template_name = 'docs/doc_category.html'
    def get_queryset(self):
        if self.request.user.is_superuser:
            return DocCategory.objects.all()
        else:
            return DocCategory.objects.filter(access__exact=self.request.user)

class DocsUpdate(UpdateView):
    form_class = DocAddForm
    success_url = '/docs/'
    template_name = 'docs/doc_update.html'
    def get_queryset(self):
        if self.request.user.is_superuser:
            return Document.objects.all()
        else:
            raise PermissionDenied

class DocsCreate(CreateView):
    form_class = DocAddForm
    template_name = 'docs/doc_create.html'
    success_url = '/docs/'

    def get(self, request, *args, **kwargs):
        context = super(DocsCreate, self).get(request, *args, **kwargs)
        if request.user.is_superuser:
            return context
        raise PermissionDenied

class DocsDeleteView(DeleteView):
    success_url = '/docs/'
    template_name = 'docs/document_confirm_delete.html'
    def get_queryset(self):
        if self.request.user.is_superuser:
            return Document.objects.all()
        else:
            raise PermissionDenied



class DocsCatUpdate(UpdateView):
    form_class = CatAddForm
    success_url = '/docs/'
    template_name = 'docs/doc_cat_update.html'
    
    def get_queryset(self):
        if self.request.user.is_superuser:
            return DocCategory.objects.all()
        else:
            raise PermissionDenied

class DocsCatCreate(CreateView):
    form_class = CatAddForm
    template_name = 'docs/doc_cat_create.html'
    success_url = '/docs/'

    def get(self, request, *args, **kwargs):
        context = super(DocsCatCreate, self).get(request, *args, **kwargs)
        if request.user.is_superuser:
            return context
        raise PermissionDenied

class DocsCatDeleteView(DeleteView):
    success_url = '/docs/'
    template_name = 'docs/doccategory_confirm_delete.html'
    def get_queryset(self):
        if self.request.user.is_superuser:
            return DocCategory.objects.all()
        else:
            raise PermissionDenied